home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…tember: Reference Library / Dev.CD Sep 98 RL1.toast / Technical Documentation / develop / develop Issue 27 / develop Issue 27 code / Internet Config Assistant / toolkit / TDrawContext.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-30  |  9.5 KB  |  521 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TDrawContext.h
  3.  
  4.     Contains:    Interface file for a QuickDraw layer class
  5.  
  6.     Written by:    Arno Gourdol
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #pragma once
  13.  
  14. #ifndef __TDRAWCONTEXT__
  15. #define __TDRAWCONTEXT__
  16.  
  17. #include "assert.h"
  18.  
  19. #include <Quickdraw.h>
  20. #include "CRect.h"
  21. #include "CColor.h"
  22.  
  23.  
  24.  
  25. //
  26. //    Class TDrawContext
  27. //
  28. //    Contains related graphical information:
  29. //        - GrafPort
  30. //        - Background and Foreground colors
  31. //        - Pen size, mode, position, pattern
  32.  
  33. class CTempRgn;
  34.  
  35. class TBitmap;
  36.  
  37. class TDrawContext
  38. {
  39.     friend class TDrawContextIterator;
  40. public:
  41.     // constructors
  42.     inline TDrawContext(void);
  43.     inline TDrawContext(GrafPtr grafPtr, UInt16 depth = 1, Boolean inColor = true);
  44.     
  45.     // destructor
  46.     inline ~TDrawContext(void);
  47.     
  48.     // accessor
  49.     inline CRect Bounds(void) const;
  50.     inline Boolean IsColor(void) const;
  51.     inline UInt16 GetDepth(void) const;
  52.     
  53.     // setters
  54.     inline void SetPenSize(GraphicalUnit size) const;
  55.     void SetHighColor(CColor color);
  56.  
  57.     inline void ConvertToScreen(CPoint& point) const;
  58.     inline void ConvertFromScreen(CPoint& point) const;
  59.     inline void ConvertToScreen(CRect& rect) const;
  60.     inline void ConvertFromScreen(CRect& rect) const;
  61.  
  62.     // Pen location
  63.     void MovePenTo(CPoint pt);
  64.     void MovePenTo(GraphicalUnit x, GraphicalUnit y);
  65.     void MovePenBy(GraphicalUnit dx, GraphicalUnit dy);
  66.     CPoint PenLocation(void) const;
  67.     
  68.     // Drawing lines
  69.     void StrokeLine(CPoint toPt);
  70.     void StrokeLine(CPoint pt0, CPoint pt1);
  71.  
  72.     // Drawing Rectangles
  73.     void StrokeRect(const CRect& rect);
  74.     void FillRect(const CRect& rect);
  75.     void InvertRect(const CRect& rect);
  76.  
  77.     // Drawing Regions
  78.     void StrokeRegion(const RgnHandle rgn);
  79.     void FillRegion(const RgnHandle rgn);
  80.  
  81.     // Drawing bitmaps
  82.     void DrawBitmap(const TBitmap* bitmap) const;
  83.     void DrawBitmap(const TBitmap* bitmap, CPoint where) const;
  84.     void DrawBitmap(const TBitmap* bitmap, CRect dstRect) const;
  85.     void DrawBitmap(const TBitmap* bitmap, CRect dstRect, RgnHandle mask) const;
  86.     void DrawBitmap(const TBitmap* bitmap, CRect srcRect, CRect dstRect) const;
  87.     
  88.     // Drawing focus rings
  89.     void StrokeFocusRing(const CRect& rect);
  90.     void StrokeFocusRing(const RgnHandle rgn);
  91.  
  92.     // 
  93.     virtual Boolean Lock(void);
  94.     virtual void Unlock(void);
  95.     
  96. protected:
  97.     inline Boolean IsColorPort(void) const;    
  98.  
  99.     GrafPtr fSavePort;        // GrafPort previously set
  100.     PenState fSavePenState;    // Pen size, mode, pattern...
  101.     RGBColor fSaveForeColor;
  102.     RGBColor fSaveBackColor;
  103.     
  104.     GrafPtr fPort;
  105.     UInt16 fDepth;
  106.     Boolean fIsColor;
  107.     
  108.     SInt16 fLockCount;
  109. };
  110.  
  111.  
  112. // Input Iterator
  113. class TDrawContextIterator
  114. {
  115. public:
  116.     inline TDrawContextIterator(void);
  117.     inline TDrawContextIterator(const CRect& area);
  118.     inline TDrawContextIterator(GrafPtr port, const CRect& area);
  119.     inline ~TDrawContextIterator();
  120.  
  121.     inline TDrawContextIterator& operator ++();
  122.     inline TDrawContext& operator *(void);
  123.     inline Boolean operator ==(const TDrawContextIterator& operand) const;
  124.     inline Boolean operator !=(const TDrawContextIterator& operand) const;
  125.     inline TDrawContextIterator& end(void) const;
  126. private:
  127.     GDHandle fCurrentDevice;
  128.     CRect fArea;
  129.     TDrawContext fDrawContext;
  130. };
  131.  
  132.  
  133. inline TDrawContextIterator&  TDrawContextIterator::end(void) const
  134. {
  135.     TDrawContextIterator nullIterator;
  136.     return nullIterator;
  137. }
  138.  
  139.  
  140. inline Boolean TDrawContextIterator::operator ==(const TDrawContextIterator& operand) const
  141. {
  142.     return (fCurrentDevice == operand.fCurrentDevice);
  143. }
  144.  
  145. inline Boolean TDrawContextIterator::operator !=(const TDrawContextIterator& operand) const
  146. {
  147.     return (fCurrentDevice != operand.fCurrentDevice);
  148. }
  149.  
  150.  
  151. inline TDrawContextIterator::TDrawContextIterator(void)
  152. {
  153.     fCurrentDevice = NULL;
  154. }
  155.  
  156.  
  157. inline TDrawContextIterator::TDrawContextIterator(const CRect& area)
  158. {
  159.     fCurrentDevice = GetDeviceList();
  160.     fArea = area;
  161.     if (fDrawContext.Lock())
  162.     {
  163.         fDrawContext.ConvertToScreen(fArea);
  164.         fDrawContext.Unlock();
  165.     }
  166.     while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)))
  167.     {
  168.         fCurrentDevice = GetNextDevice(fCurrentDevice);
  169.     }
  170.     
  171.     if (fCurrentDevice != NULL)
  172.     {
  173.         fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
  174.         fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
  175.     }
  176. }
  177.  
  178. inline TDrawContextIterator::TDrawContextIterator(GrafPtr port, const CRect& area) :
  179.     fDrawContext(port)
  180. {
  181.     fCurrentDevice = GetDeviceList();
  182.     fArea = area;
  183.     if (fDrawContext.Lock())
  184.     {
  185.         fDrawContext.ConvertToScreen(fArea);
  186.         fDrawContext.Unlock();
  187.     }
  188.     
  189.     while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)))
  190.     {
  191.         fCurrentDevice = GetNextDevice(fCurrentDevice);
  192.     }
  193.     
  194.     if (fCurrentDevice != NULL)
  195.     {
  196.         fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
  197.         fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
  198.     }
  199. }
  200.  
  201.  
  202.  
  203. inline TDrawContextIterator::~TDrawContextIterator()
  204. {
  205. }
  206.  
  207.  
  208. inline TDrawContextIterator& TDrawContextIterator::operator ++()
  209. {
  210.     
  211.     // Look at the next device
  212.     do
  213.     {
  214.         fCurrentDevice = GetNextDevice(fCurrentDevice);
  215.     } while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)));
  216.     
  217.     if (fCurrentDevice != NULL)
  218.     {
  219.         fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
  220.         fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
  221.     }
  222.     return *this;
  223. }
  224.  
  225.  
  226. inline TDrawContext& TDrawContextIterator::operator *(void)
  227. {
  228.     return fDrawContext;
  229. }
  230.  
  231.  
  232.  
  233. //
  234. //    Inlines for TDrawContext
  235. //
  236.  
  237. inline TDrawContext::TDrawContext(void) :
  238.     fSavePort(NULL),
  239.     fLockCount(0)
  240. {
  241.     GetPort(&fPort);
  242.     fDepth = 1;
  243.     fIsColor = false;
  244. }
  245.  
  246. inline TDrawContext::TDrawContext(GrafPtr grafPtr, UInt16 depth, Boolean inColor) :
  247.     fSavePort(NULL),
  248.     fLockCount(0)
  249. {
  250.     fPort = grafPtr;
  251.     fDepth = depth;
  252.     fIsColor = inColor;
  253. }
  254.  
  255. inline TDrawContext::~TDrawContext()
  256. {
  257.     assert(fLockCount == 0);
  258. }
  259.  
  260. inline CRect TDrawContext::Bounds(void) const
  261. {
  262.     CRect result(fPort->portRect);
  263.     ConvertToScreen(result);
  264.     return result;
  265. }
  266.  
  267. inline Boolean TDrawContext::IsColor(void) const
  268. {
  269.     return fIsColor;
  270. }
  271.  
  272. inline Boolean TDrawContext::IsColorPort(void) const
  273. {
  274.     return (fPort->portBits.rowBytes & 0xC000) == 0xC000;
  275. }
  276.  
  277. inline UInt16 TDrawContext::GetDepth(void) const
  278. {
  279.     return fDepth;
  280. }
  281.  
  282. inline void TDrawContext::SetPenSize(GraphicalUnit size) const
  283. {
  284.     assert(fLockCount > 0);
  285.  
  286.     ::PenSize(size, size);
  287. }
  288.  
  289. inline void TDrawContext::ConvertToScreen(CPoint& point) const
  290. {
  291.     assert(fLockCount > 0);
  292.  
  293.     ::LocalToGlobal(point);
  294. }
  295.  
  296. inline void TDrawContext::ConvertFromScreen(CPoint& point) const
  297. {
  298.     assert(fLockCount > 0);
  299.  
  300.     ::GlobalToLocal(point);
  301. }
  302.  
  303. inline void TDrawContext::ConvertToScreen(CRect& rect) const
  304. {
  305.     assert(fLockCount > 0);
  306.  
  307.     CPoint temp = rect.LeftTop();
  308.     ::LocalToGlobal(temp);
  309.     rect.SetLeftTop(temp);
  310.     temp = rect.RightBottom();
  311.     ::LocalToGlobal(temp);
  312.     rect.SetRightBottom(temp);
  313. }
  314.  
  315.  
  316. inline void TDrawContext::ConvertFromScreen(CRect& rect) const
  317. {
  318.     assert(fLockCount > 0);
  319.  
  320.     // ??? Doesn't work LefTop() returns a cons See ConverToScreen
  321.     ::GlobalToLocal(rect.LeftTop());
  322.     ::GlobalToLocal(rect.RightBottom());
  323. }
  324.  
  325.  
  326. //
  327. //    CClip class
  328. //
  329. // Class to save, restore and change the clipping region
  330.  
  331. class CClip
  332. {
  333. public:
  334.     inline CClip();
  335.     inline ~CClip();
  336.     inline void Save();
  337.     inline void Restore();
  338.     inline void Set(RgnHandle clipRgn);
  339.     inline void Set(const Rect& clipRect);
  340.     inline void Set(const CRect& clipRect);
  341.  
  342. private:
  343.     RgnHandle fClipRgn;
  344. #ifndef NDEBUG
  345.     short fSaveCount;
  346. #endif
  347.  
  348. };
  349.  
  350. //
  351. // CTempRgn class
  352. //
  353. // Class managing a temporary region. Useful to pass 
  354. // rects to routines require regions.
  355. // Example: 
  356. //                CTempRgn aRgn(r);
  357. //                SetClip(aRgn);
  358.  
  359. class CTempRgn
  360. {
  361. public:
  362. // constructors
  363.     inline CTempRgn();
  364.     inline CTempRgn(const Rect& area);
  365.     inline CTempRgn(const CTempRgn& rgn); // copy constructor
  366.     inline CTempRgn(const RgnHandle rgn);        
  367.  
  368. // destructor
  369.     inline ~CTempRgn(void);        
  370.  
  371. // operators
  372.     inline operator RgnHandle(void);        
  373.  
  374. // modifiers
  375.     inline void Set(const CRect& bounds);
  376.     inline void LocalToGlobal(void);        
  377.      inline void GlobalToLocal(void);
  378.     
  379. // selectors
  380.     inline CRect Bounds(void) const;
  381.  
  382. private:
  383. // member objects
  384.     RgnHandle fTempRgn;        
  385. };
  386.  
  387.  
  388. //
  389. //    Inlines for CClip
  390. //
  391.  
  392. inline CClip::CClip()
  393. {
  394.     fClipRgn = NULL;
  395. #ifndef NDEBUG
  396.     fSaveCount = 0;
  397. #endif
  398.  
  399. }
  400.  
  401.  
  402. inline CClip::~CClip()
  403. {
  404.     if (fClipRgn != NULL)
  405.         DisposeRgn(fClipRgn);
  406. #ifndef NDEBUG
  407.     assert(fSaveCount == 0);
  408. #endif
  409.  
  410. }
  411.  
  412.  
  413. inline void CClip::Set(RgnHandle clipRgn)
  414. {
  415.     SetClip(clipRgn);
  416. }
  417.  
  418.  
  419.  
  420. inline void CClip::Set(const Rect& clipRect)
  421. {
  422.     ClipRect(&clipRect);
  423. }
  424.  
  425.  
  426. inline void CClip::Set(const CRect& clipRect)
  427. {
  428.     ClipRect(clipRect);
  429. }
  430.  
  431.  
  432.  
  433. inline void CClip::Save()
  434. {
  435.     if (fClipRgn == NULL)
  436.         fClipRgn = NewRgn();
  437.     GetClip(fClipRgn);
  438. #ifndef NDEBUG
  439.     fSaveCount++;
  440. #endif
  441.  
  442. }
  443.  
  444.  
  445. inline void CClip::Restore()
  446. {
  447.     SetClip(fClipRgn);
  448. #ifndef NDEBUG
  449.     fSaveCount--;
  450. #endif
  451.  
  452. }
  453.  
  454.  
  455. //
  456. // Inlines for CTempRgn
  457. //
  458.  
  459.  
  460. inline CTempRgn::operator RgnHandle(void)
  461. {
  462.     return fTempRgn;
  463. }
  464.  
  465. inline CTempRgn::CTempRgn(const Rect& area)
  466. {
  467.     fTempRgn = NewRgn();
  468.     RectRgn(fTempRgn, &area);
  469. }
  470.  
  471. inline CTempRgn::CTempRgn(const CTempRgn& rgn)
  472. {
  473.     fTempRgn = NewRgn();
  474.     CopyRgn((CTempRgn&)rgn, fTempRgn);        // (-) const
  475. }
  476.  
  477. inline CTempRgn::CTempRgn(const RgnHandle rgn)
  478. {
  479.     fTempRgn = NewRgn();
  480.     CopyRgn((RgnHandle)rgn, fTempRgn);// (-)
  481. }
  482.  
  483. inline CTempRgn::CTempRgn()
  484. {
  485.     fTempRgn = NewRgn();
  486. }
  487.  
  488.  
  489. inline CTempRgn::~CTempRgn(void)
  490. {
  491.     assert(fTempRgn != NULL);
  492.     DisposeRgn(fTempRgn);
  493. }
  494.  
  495. inline void CTempRgn::Set(const CRect& bounds)
  496. {
  497.     RectRgn(fTempRgn, bounds);
  498. }
  499.  
  500. inline void CTempRgn::LocalToGlobal(void)
  501. {
  502.     ::LocalToGlobal((Point *) &((*fTempRgn)->rgnBBox.top));
  503.     ::LocalToGlobal((Point *) &((*fTempRgn)->rgnBBox.bottom));
  504. }
  505.  
  506.  
  507. inline void CTempRgn::GlobalToLocal(void)
  508. {
  509.     ::GlobalToLocal((Point *) &((*fTempRgn)->rgnBBox.top));
  510.     ::GlobalToLocal((Point *) &((*fTempRgn)->rgnBBox.bottom));
  511. }
  512.  
  513. inline CRect CTempRgn::Bounds(void) const
  514. {
  515.     return (*fTempRgn)->rgnBBox;
  516. }
  517.  
  518.  
  519. #endif
  520.  
  521.